package com.mao.action.product;

import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.product.jsonVoProduct;
import com.mao.model.product.Product;
import com.mao.model.product.ProductClass;
import com.mao.model.store.Store;
import com.mao.model.user.User;
import com.mao.model.warehouse.WareHouse;
import com.mao.service.impl.product.ProductCondition;
import com.mao.service.product.IProductClassService;
import com.mao.service.product.IProductService;
import com.mao.service.productstock.IProductStockService;
import com.mao.service.searchvoucher.ISearchVoucherService;
import com.mao.service.warehouse.IWareHouseService;
import com.mao.util.SessionHelper;
import com.mao.util.Validator;

public class ProductManageAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private Product pro;

	private ProductClass pc;

	private IProductService<Product> productService;

	private IProductClassService<Product> productClassService;
	
	private ISearchVoucherService searchVoucherService;

	private IProductStockService productStockService;

	private IWareHouseService wareHouseService;

	private Map<String, Object> result;

	private List<Product> productList;

	private Integer page = 0;

	private Integer rows = 0;

	private String proClassId = "0";

	private ProductCondition productCondition;

	private Long pcId;

	private String proName;

	private boolean flag = true;

	@Override
	public String execute() {
		return SUCCESS;
	}

	public String getAllProducts() {
		if (productCondition == null) {
			productCondition = new ProductCondition();
		}
		result = new HashMap<String, Object>();
		User user = SessionHelper.getSessionUser();
		productCondition.setStoreid(user.getStore().getId());
		if (proName != null)
			productCondition.setName(proName);
		if (pcId != null) {
			productCondition.setPcid(pcId);
			pc = productClassService.getProductClassById(pcId);
			productCondition.setTreePath(pc.getTreePath());
		}
		productList = productService.getAllProducts(productCondition, page,
				rows);
		List<jsonVoProduct> jsonProsList = new ArrayList<jsonVoProduct>();
		jsonVoProduct jsonPro = null;
		Map<String, Object> attsMap = null;
		for (int i = 0; i < productList.size(); i++) {
			jsonPro = new jsonVoProduct();
			attsMap = new HashMap<String, Object>();
			Product p = productList.get(i);
			jsonPro.setId(p.getId());
			jsonPro.setName(p.getName());
			jsonPro.setProNo(p.getProNo());
			jsonPro.setSpec(p.getSpec());
			jsonPro.setNote(p.getNote());
			jsonPro.setRetailPrice(p.getRetailPrice());
			jsonPro.setLowestPrice(p.getLowestPrice());
			jsonPro.setInitStock(p.getInitStock());
			if(p.getInitWareHouse() != null){
				jsonPro.setWareHouseId(p.getInitWareHouse().getId());
				jsonPro.setWareHouseName(p.getInitWareHouse().getName());
			}
			jsonPro.setProClassName(p.getProClass().getName());
			attsMap.put("proClassId", p.getProClass().getId());
			attsMap.put("proClassName", p.getProClass().getName());
			jsonPro.setAttributes(attsMap);
			jsonProsList.add(jsonPro);

		}
		result.put("total", productService.getAllProductCount(productCondition));
		result.put("rows", jsonProsList);
		return SUCCESS;
	}

	public String jsonSaveProduct() {
		DecimalFormat df = new DecimalFormat("#.00");
		Store store = SessionHelper.getSessionUser().getStore();
		Long pcId = pro.getProClass().getId();
		if (pcId != null) {
			pc = productClassService.getProductClassById(pcId);
		}
		Product newPro = new Product();
		newPro.setName(pro.getName());
		newPro.setProNo(pro.getProNo());
		newPro.setSpec(pro.getSpec());
		newPro.setRetailPrice(new BigDecimal(df.format(pro.getRetailPrice())));
		newPro.setLowestPrice(new BigDecimal(df.format(pro.getLowestPrice())));
		newPro.setNote(pro.getNote());
		newPro.setProClass(pc);
		newPro.setCreateTime(new Date());
		newPro.setStore(SessionHelper.getSessionUser().getStore());
		// 当前库存
		if(!Validator.isEmpty(pro.getInitStock()) && pro.getInitWareHouse()!=null){
			WareHouse wareHouse = wareHouseService.getWareHouseById(pro.getInitWareHouse().getId());
			newPro.setInitWareHouse(wareHouse);
			newPro.setInitStock(pro.getInitStock());
		}
		pro = productService.saveProduct(newPro);
		return SUCCESS;
	}

	public String jsonUpdateProduct() {
		DecimalFormat df = new DecimalFormat("#.00");
		Store store = SessionHelper.getSessionUser().getStore();
		Product updatePro = productService.getProductById(pro.getId());
		if (pro.getProClass().getId() != null) {
			pc = productClassService.getProductClassById(pro.getProClass()
					.getId());
		}
		updatePro.setProClass(pc);
		updatePro.setName(pro.getName());
		updatePro.setProNo(pro.getProNo());
		updatePro.setSpec(pro.getSpec());
		updatePro.setNote(pro.getNote());
		updatePro.setLowestPrice(new BigDecimal(df.format(pro.getLowestPrice())));
		updatePro.setRetailPrice(new BigDecimal(df.format(pro.getRetailPrice())));
		updatePro.setStore(SessionHelper.getSessionUser().getStore());
		// 当前库存
		WareHouse initWareHouse = null;
		if(!Validator.isEmpty(pro.getInitStock()) && pro.getInitWareHouse()!=null){
			initWareHouse = wareHouseService.getWareHouseById(pro.getInitWareHouse().getId());
		}
		updatePro.setInitStock(pro.getInitStock());
		updatePro.setInitWareHouse(initWareHouse);
		productService.updateProduct(updatePro);
		
		return SUCCESS;
	}

	public String getProductsByProClassId() {
		productCondition = new ProductCondition();
		productCondition.setStoreid(SessionHelper.getSessionUser().getStore()
				.getId());
		if (proName != null) {
			productCondition.setName(proName);
		}
		if (!proClassId.equals("0")) {
			ProductClass pc = productClassService.getProductClassById(Long
					.valueOf(proClassId));
			productCondition.setTreePath(pc.getTreePath());
		}
		productList = productService.getProductsByProClass(productCondition,
				page, rows);
		List<jsonVoProduct> jsonProsList = new ArrayList<jsonVoProduct>();
		jsonVoProduct jsonPro = null;
		Map<String, Object> attsMap = null;
		for (int i = 0; i < productList.size(); i++) {
			jsonPro = new jsonVoProduct();
			attsMap = new HashMap<String, Object>();
			Product p = productList.get(i);
			jsonPro.setId(p.getId());
			jsonPro.setName(p.getName());
			jsonPro.setProNo(p.getProNo());
			jsonPro.setSpec(p.getSpec());
			jsonPro.setNote(p.getNote());
			jsonPro.setRetailPrice(p.getRetailPrice());
			jsonPro.setLowestPrice(p.getLowestPrice());
			jsonPro.setProClassName(p.getProClass().getName());
			jsonPro.setStock(searchVoucherService.getStockByProductId(p.getId(), null, null, null));
			attsMap.put("proClassId", p.getProClass().getId());
			attsMap.put("proClassName", p.getProClass().getName());
			jsonPro.setAttributes(attsMap);
			jsonProsList.add(jsonPro);
		}
		result = new HashMap<String, Object>();
		result.put("total",
				productService.getProductsByProClassCount(productCondition));
		result.put("rows", jsonProsList);
		return SUCCESS;
	}

	public String deletePro() {
		Product updatePro = productService.getProductById(pro.getId());
		updatePro.setStatus(Product.STATUS_DELETED);
		productService.updateProduct(updatePro);
		return SUCCESS;
	}

	// 导出商品
	public String exportExcel() {
		productCondition = new ProductCondition();
		// if (!Validator.isEmpty(proName))//不用判断，因为在Dao里面已经做了判断
		productCondition.setName(proName);
		if (pcId != null) {
			productCondition.setPcid(pcId);
			pc = productClassService.getProductClassById(pcId);
			productCondition.setTreePath(pc.getTreePath());
		}
		productCondition.setStoreid(SessionHelper.getSessionUser().getStore()
				.getId());
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="
					+ new String("商品列表".getBytes("GB2312"), "iso8859_1")
					+ ".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型

			productService.exportExcel(productCondition, os);
		} catch (Exception e) {
			System.out.println(e);
			flag = false;
		}
		return SUCCESS;
	}

	public String jsonGetProducts() {
		return SUCCESS;
	}

	public Product getPro() {
		return pro;
	}

	public void setPro(Product pro) {
		this.pro = pro;
	}

	public IProductService<Product> getProductService() {
		return productService;
	}

	public void setProductService(IProductService<Product> productService) {
		this.productService = productService;
	}

	public IProductClassService<Product> getProductClassService() {
		return productClassService;
	}

	public void setProductClassService(
			IProductClassService<Product> productClassService) {
		this.productClassService = productClassService;
	}

	public ProductClass getPc() {
		return pc;
	}

	public void setPc(ProductClass pc) {
		this.pc = pc;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public String getProClassId() {
		return proClassId;
	}

	public void setProClassId(String proClassId) {
		this.proClassId = proClassId;
	}

	public ProductCondition getProductCondition() {
		return productCondition;
	}

	public void setProductCondition(ProductCondition productCondition) {
		this.productCondition = productCondition;
	}

	public Long getPcId() {
		return pcId;
	}

	public void setPcId(Long pcId) {
		this.pcId = pcId;
	}

	public String getProName() {
		return proName;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public IProductStockService getProductStockService() {
		return productStockService;
	}

	public void setProductStockService(IProductStockService productStockService) {
		this.productStockService = productStockService;
	}

	public IWareHouseService getWareHouseService() {
		return wareHouseService;
	}

	public void setWareHouseService(IWareHouseService wareHouseService) {
		this.wareHouseService = wareHouseService;
	}

	public ISearchVoucherService getSearchVoucherService() {
		return searchVoucherService;
	}

	public void setSearchVoucherService(ISearchVoucherService searchVoucherService) {
		this.searchVoucherService = searchVoucherService;
	}
	

}
